package com.masterclient.app.modules.order.contract;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.order.PaySuccessBean;
import com.eoner.baselibrary.bean.order.PresentationShareBean;
import com.eoner.commonbean.popup.CommonPopUpBean;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.httpservice.RetrofitUtil2;

import java.util.List;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/08/25
*/
public class PaySuccessPresenter extends PaySuccessContract.Presenter{

    @Override
    public void getShareData(String order_no) {
        Observable<PresentationShareBean> observable = RetrofitUtil2.getInstance().getPresentationShare(order_no);
        Disposable disposable = toSubscribe(observable, (Consumer<PresentationShareBean>) bean -> {
            if (bean!=null && bean.getData()!=null) {
                mView.getShareDataSuccess(bean);
            } else {
                ToastUtil.showToast("分享失败");
            }
        },  new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                ToastUtil.showToast("请检查网络");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void paySuccess(String trade_no , String type) {
        Observable<PaySuccessBean> observable = RetrofitUtil.getInstance().paySuccess(trade_no ,type);
        Disposable disposable = toSubscribe(observable, new Consumer<PaySuccessBean>() {
            @Override
            public void accept(@NonNull PaySuccessBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getPaySuccess(bean.getData());
                }else {
                    mView.getPaySuccessFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void getPopUpData(String page_type, String order_type,String trade_no) {
        JSONObject popUpParam = new JSONObject();
        popUpParam.put("location",order_type);
        popUpParam.put("trade_no",trade_no);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("scene",page_type);
            jsonObject.put("scene_value",popUpParam);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());

        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().commonDialogData(body), new Consumer<CommonBaseResponse<List<CommonPopUpBean>>>() {
            @Override
            public void accept(CommonBaseResponse<List<CommonPopUpBean>> dialogBean) throws Exception {
                if (dialogBean.getCode() == 0){
                    mView.getDialogSuccess(dialogBean.getData());
                }
            }
        },new ThrowableConsumer()));
    }

}